#!/usr/bin/env python
#
# Copy this in ~/.gimp-2.x/plug-ins/
# and restart gimp

import math
import os
import sys
from gimpfu import *

def BigPixels(img, layer, multiplier, drawgrid, drawnums, fontname, fontsize):
  """
  Make the image bigger and put lines in 
  Params:
    multiplier: x times larger image
    drawgrid: Draw a grid (very useful)
    drawnums: Draw numbers on the bottom & left starting at 1
    fontname: font to use for numbers
    fontsize: size to user for numbers
  """
  drawable = pdb.gimp_image_flatten(img)
  w, h = pdb.gimp_image_width(img), pdb.gimp_image_height(img)
  pdb.gimp_drawable_transform_scale(drawable, 0.0, 0.0, 
                                    w * multiplier, h * multiplier, 
                                    TRANSFORM_FORWARD,
                                    INTERPOLATION_NONE,
                                    1, # Supersample? 
                                    3, # Level of recursion
                                    0) # Clip results
  pdb.gimp_image_resize_to_layers(img)
  w, h = pdb.gimp_image_width(img), pdb.gimp_image_height(img)
  img.disable_undo()
  if drawnums:
    drawable = ResizeBigger(img, multiplier * 2, multiplier * 2)
    w, h = pdb.gimp_image_width(img), pdb.gimp_image_height(img)
    DrawNumbers(img, drawable, w, h, multiplier, fontname, fontsize)
  if drawgrid:
    DrawLines(img, drawable, w, h, multiplier)
  img.enable_undo()
  gimp.displays_flush()

def DrawLines(img, drawable, w, h, multiplier):
  x = multiplier
  black = (0, 0, 0)
  num = 0
  while x < w:
    DrawRect(img, drawable, black, x, 1, LineSize(x, 4, multiplier), h)
    x += multiplier
    num += 1
  y = multiplier
  while y < h:
    DrawRect(img, drawable, black, 1, y, w, LineSize(y, 3, multiplier))
    y += multiplier
  
def LineSize(num, offset, multiplier):
  if (((num / multiplier) + offset) % 5) == 0:
    return 2
  return 1

def DrawRect(img, drawable, color, x, y, w, h):
  pdb.gimp_selection_none(img)
  pdb.gimp_context_set_foreground(color)
  pdb.gimp_rect_select(img, x, y, w, h,
                       CHANNEL_OP_ADD,
                       0, 0)
  pdb.gimp_edit_bucket_fill(drawable, FG_BUCKET_FILL, NORMAL_MODE, 
                            100, # Opacity
                            0, # threshold
                            0, # sample_merged,
                            0, # x, y
                            0) 
  pdb.gimp_selection_none(img)

def DrawNumbers(img, drawable, w, h, multiplier, fontname, fontsize):
  x = multiplier + multiplier / 2
  y = h
  num = 1
  while x < w - multiplier:
    DrawNumberCentered(img, drawable, x, y, num, fontname, fontsize)
    num += 1
    x += multiplier

  x = multiplier / 2
  y = h - multiplier
  num = 1
  while y > multiplier:
    DrawNumberCentered(img, drawable, x, y, num, fontname, fontsize)
    num += 1
    y -= multiplier

def DrawNumberCentered(img, drawable, x, y, num, fontname, fontsize):
  txt = str(num)
  (txt_w, txt_h, ascent, descent) = pdb.gimp_text_get_extents_fontname( 
      txt, fontsize, PIXELS, fontname) 
  txtfloat = pdb.gimp_text_fontname(img, drawable, 
      x - txt_w / 2, y - txt_h, 
      txt, -1, # Border
      True, # AntiAliased
      fontsize, PIXELS, fontname) 
  # Merge with background
  pdb.gimp_floating_sel_anchor(txtfloat)

def ResizeBigger(img, added_width, added_height):
  w, h = pdb.gimp_image_width(img), pdb.gimp_image_height(img)
  pdb.gimp_image_resize(img, w + added_width, h + added_height, 
                        added_width / 2, added_height / 2)
  return pdb.gimp_image_flatten(img)

register(
        "python_fu_big_pixels",
        "Tool to make large pixesl for pixelblocks, say",
        "Tool to make large pixesl for pixelblocks, say",
        "Scott Kirkwood",
        "Scott Kirkwood",
        "2008",
        "<Image>/Python-Fu/Big Pixels...",
        "",
        [
                (PF_FLOAT, "multiplier", "How much bigger", 16.0),
                (PF_BOOL, "drawgrid", "Draw Grid - very useful", True),
                (PF_BOOL, "drawnums", "Draw Numbers", True),
                (PF_STRING, "fontname", "Font Name", "Monospace"),
                (PF_INT, "fontsize", "Font Size", 9),
        ],
        [],
        BigPixels
        )

main()
